package ru.mcfr.oxygen.framework.operations;

import org.apache.log4j.Logger;
import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.access.AuthorTableAccess;
import ro.sync.ecss.extensions.api.node.AttrValue;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;

import javax.swing.text.BadLocationException;
import java.util.List;

public class AttributOperation implements AuthorOperation {

    private static Logger logger = Logger.getLogger(AttributOperation.class.getName());


    public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
            throws IllegalArgumentException, AuthorOperationException {
        AuthorDocumentController controller = authorAccess.getDocumentController();
        AuthorSchemaManager manager = authorAccess.getDocumentController().getAuthorSchemaManager();
        AuthorTableAccess tableContainer = authorAccess.getTableAccess();

        String attrName = "";
        try {
            attrName = args.getArgumentValue("attr name").toString();
        } catch (Exception e) {
        }
        String attrValue = "";
        try {
            attrValue = args.getArgumentValue("attr value").toString();
        } catch (Exception e) {
        }
        String op_type = "";
        try {
            op_type = args.getArgumentValue("op_type").toString();
        } catch (Exception e) {
        }

        try {
            int selStart = controller.getNodeAtOffset(authorAccess.getEditorAccess().getSelectionStart()).getStartOffset();
            int selEnd = controller.getNodeAtOffset(authorAccess.getEditorAccess().getSelectionEnd()).getEndOffset();

            AuthorDocumentFragment fragment = controller.createDocumentFragment(selStart, selEnd);
            List<AuthorNode> nodes = fragment.getContentNodes();
            for (AuthorNode node : nodes) {
                if (node.getType() == node.NODE_TYPE_ELEMENT) {
                    AuthorElement ae = (AuthorElement) node;
                    if (op_type.equals("delete"))
                        ae.removeAttribute(attrName);
                    if (op_type.equals("change"))
                        ae.setAttribute(attrName, new AttrValue(attrValue));
                    if (op_type.equals("set_to_null"))
                        ae.setAttribute(attrName, new AttrValue(""));
                }
            }

        } catch (BadLocationException e) {
            logger.error(e.getMessage());
        }

    }


    public ArgumentDescriptor[] getArguments() {
        ArgumentDescriptor[] res = {
                new ArgumentDescriptor("attr name", ArgumentDescriptor.TYPE_STRING, ""),
                new ArgumentDescriptor("attr value", ArgumentDescriptor.TYPE_STRING, ""),
                new ArgumentDescriptor("op_type", ArgumentDescriptor.TYPE_STRING, ""),
        };

        return res;
    }


    public String getDescription() {
        // TODO Auto-generated method stub
        return null;
    }

}
